'''
https://leetcode.cn/problems/number-of-1-bits/?envType=study-plan-v2&envId=top-interview-150
'''


class Solution:
    def hammingWeight(self, n: int) -> int:
        res = 0
        while n:
            res += 1
            last_1 = n & (-n)
            n ^= last_1
        return res
    def hammingWeight2(self, n: int) -> int:
        res = 0
        while n:
            res += 1
            n &= n-1
        return res